// pages/myMoneyRecord/myMoneyRecord.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    req:false,
    showLoadMore: false,
    list: [],
    search: {
      pageNumber: 1,
      pageSize: 10,
      totalPages: 0
    },
    payType: {
      0: '收款',
      1: '提现',
      2: '退款',
      3: '部分退款',
      4: '活动余额转移到个人钱包',
      5: '充值',
      6: '付款',
      7: '发红包',
      8: '收红包',
      9: '退还红包',
      10: '回礼'
    }
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  refreshList: function() {
    var that = this;
    app.getUserInfo(function(userInfo) {
      that.toggle("search.uid", userInfo.id);
      that.toggle("search.pageNumber", 1);
      that.toggle("search.totalPages", 0);
      that.toggle("showLoadMore", false);
      that.list();
    });
  },
  list: function(append) {
    var that = this;
    that.toggle("req", false);
    app.req({
      url: "orgmini/account/expenseCalendars",
      data: that.data.search,
      success: function(res) {
        if (200 == res.code) {
          
          var oriList = that.data.list;
          var newList = res.data.actAccountRecord.rows;
          for (var i of newList) {
            i.createTimeShow = i.createTime.substr(5, 11).replace("-", "月").replace(" ", "日 ");
          }
          that.toggle("list", append ? oriList.concat(newList) : newList);
          that.toggle("search.totalPages", res.data.actAccountRecord.totalPages);
          that.toggle("showLoadMore", res.data.actAccountRecord.totalPages * 1 > that.data.search.pageNumber * 1);
          that.toggle("req", true);
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.refreshList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.refreshList();
  },
  loadMore: function() {
    var that = this;
    var param = that.data.search;
    if (param.pageNumber < param.totalPages) {
      that.toggle("search.pageNumber", param.pageNumber * 1 + 1);
      that.toggle("showLoadMore", true);
      that.list(true);
    } else {
      that.toggle("showLoadMore", false);
    }
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    var param = that.data.search;
    if (param.pageNumber < param.totalPages) {
      that.toggle("search.pageNumber", param.pageNumber * 1 + 1);
      that.toggle("showLoadMore", true);
      that.list(true);
    } else {
      that.toggle("showLoadMore", false);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})